﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using TierApplication;

namespace OrderMgr.OrderInfo
{
    public partial class frmSaleShipStockNeed : TierService.frmTemplate
    {
        public string _OrderID;
        public Utility.OrderType _OrderType;
        public string _IsStock = "F";

        public frmSaleShipStockNeed()
        {
            InitializeComponent();
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");

            //if (_IsStock == "Y")
            //{
            //    txtOrderStatus.Enabled = false;
            //}
        }

        private void frmBuySearchOrder_Load(object sender, EventArgs e)
        {
            initForm();

            switch (_OrderType)
            {
                case Utility.OrderType.Buy:
                    break;
                case Utility.OrderType.BuyToStock:
                    break;
                case Utility.OrderType.BuyCancelStock:
                    break;
                case Utility.OrderType.SaleCancel:
                    break;
                case Utility.OrderType.StockTake:
                    break;
                case Utility.OrderType.Transfer:
                    break;
                case Utility.OrderType.Wastage:
                    break;
                case Utility.OrderType.Sale:
                    break;
                case Utility.OrderType.SaleStock:
                    //
                    this.Text = "未完成出库销售单查询";
                    ckbOrderUserID.Text = "业务员";
                    clOrderActQuantity.Caption = "销售总数量";
                    clStockSumCount.Caption = "已出库数量";
                    clOrderID.Caption = "销售单号";
                    break;
                case Utility.OrderType.SaleInvoice:
                    break;
                case Utility.OrderType.SaleReturnFee:
                    break;
                case Utility.OrderType.Borrow:
                    break;
                case Utility.OrderType.BorrowToStock:
                    break;
                case Utility.OrderType.BorrowSendBack:
                    //
                    this.Text = "未归还借入单查询";
                    ckbOrderUserID.Text = "借入人";
                    clOrderActQuantity.Caption = "借入总数量";
                    clStockSumCount.Caption = "已归还数量";
                    clOrderID.Caption = "借入单号";
                    break;
                case Utility.OrderType.Loan:
                    break;
                case Utility.OrderType.LoanStock:
                    this.Text = "未入库借出单查询";
                    ckbOrderUserID.Text = "业务员";
                    clOrderActQuantity.Caption = "借出总数量";
                    clStockSumCount.Caption = "已出库数量";
                    clOrderID.Caption = "销售单号";
                    break;
                case Utility.OrderType.LoanReturnBackToStock:
                    break;
                case Utility.OrderType.None:
                    break;
                default:
                    break;
            }


        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ls_where = "";


            switch (_OrderType)
            {
                case Utility.OrderType.Buy:
                    break;
                case Utility.OrderType.BuyToStock:
                    break;
                case Utility.OrderType.BuyCancelStock:
                    break;
                case Utility.OrderType.SaleCancel:
                    break;
                case Utility.OrderType.StockTake:
                    break;
                case Utility.OrderType.Transfer:
                    break;
                case Utility.OrderType.Wastage:
                    break;
                case Utility.OrderType.Sale:
                    break;
                case Utility.OrderType.SaleStock:
                    //
                    ls_where = " and a.OrderType ='" + (int)Utility.OrderType.Sale + "'";
                    ls_where = ls_where + " and a.OrderStatus >='" + (int)Utility.OrderStatus.S2Audited + "'";
                    ls_where = ls_where + " and IsNull(StockSumCount,0)<IsNull(OrderActQuantity,0)";
                    break;
                case Utility.OrderType.SaleInvoice:
                    break;
                case Utility.OrderType.SaleReturnFee:
                    break;
                case Utility.OrderType.Borrow:
                    break;
                case Utility.OrderType.BorrowToStock:
                    break;
                case Utility.OrderType.BorrowSendBack:
                    //
                    ls_where = " and a.OrderType ='" + (int)Utility.OrderType.Borrow + "'";
                    ls_where = ls_where + " and a.OrderStatus >='" + (int)Utility.OrderStatus.S2Audited + "'";
                    ls_where = ls_where + " and IsNull(StockSumCount,0)<IsNull(OrderActQuantity,0)";
                    break;
                case Utility.OrderType.Loan:

                    break;
                case Utility.OrderType.LoanStock:
                    ls_where = " and a.OrderType ='" + (int)Utility.OrderType.Loan + "'";
                    ls_where = ls_where + " and a.OrderStatus >='" + (int)Utility.OrderStatus.S2Audited + "'";
                    ls_where = ls_where + " and IsNull(StockSumCount,0)<IsNull(OrderActQuantity,0)";
                    break;
                case Utility.OrderType.LoanReturnBackToStock:
                    break;
                case Utility.OrderType.None:
                    break;
                default:
                    ls_where = ls_where + " and a.OrderStatus >='" + (int)Utility.OrderStatus.S2Audited + "'";
                    ls_where = ls_where + " and IsNull(StockSumCount,0)<IsNull(OrderActQuantity,0)";
                    break;
            }

            if ((txtOrderID.Text != "") && (ckbOrderID.Checked))
                ls_where = ls_where + " and a.OrderID like '%" + txtOrderID.Text + "%'";

            if ((txtDepotID.Text.Trim() != "") && (ckbDepotID.Checked))
                ls_where = ls_where + " and a.DepotID = '" + (string)txtDepotID.EditValue + "'";

            if ((txtOrderUserID.Text.Trim() != "") && (ckbOrderUserID.Checked))
                ls_where = ls_where + " and a.OrderUserID = '" + (string)txtOrderUserID.EditValue + "'";

            if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            {
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            }

            T_Order mOrder = new T_Order();
            DataSet dsOrder;

            switch (_OrderType)
            {
                case Utility.OrderType.SaleStock:
                    dsOrder = mOrder.GetSaleList(ls_where);
                    break;
                case Utility.OrderType.BorrowSendBack:
                    dsOrder = mOrder.GetBorrowList(ls_where);
                    break;
                default:
                    dsOrder = mOrder.GetSaleList(ls_where);
                    break;
            }
            gcOrder.DataSource = dsOrder.Tables[0];
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvOrder, "订单信息");
        }



        private void gvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmBuysDetail fDetail = new frmBuysDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.Buy;
            fDetail.ShowDialog();

            btnSearch_Click(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            frmBuysDetail fDetail = new frmBuysDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            fDetail._AuditStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.Buy;
            fDetail._OrderID = sID;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }
            btnSearch_Click(null, null);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            frmBuysDetail fDetail = new frmBuysDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            fDetail._OrderType = Utility.OrderType.Buy;
            fDetail.gFuncType = gFuncType;
            //if (gFuncType == Utility.FuncType.Select)
            fDetail._AuditStatus = Utility.OrderStatus.All;
            //else
            //    fDetail._AuditStatus = Utility.OrderStatus.Input ;
            fDetail._OrderID = sID;
            fDetail.ShowDialog();
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {

        }

        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {
            if (gFuncType == Utility.FuncType.Select)
            {
                if (gvOrder.RowCount < 1) return;
                DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
                _OrderID = dr["OrderID"].ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;

            }
            else { btnQuery_Click(null, null); }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此订单记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            T_Order mOrder = new T_Order();
            mOrder.Delete(sID);

            btnSearch_Click(null, null);
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void gvOrder_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                DataRow dr = gvOrder.GetDataRow(e.RowHandle);
                string OrderActQuantity, StockSumCount;
                OrderActQuantity = dr["OrderActQuantity"].ToString();
                StockSumCount = dr["StockSumCount"].ToString();
                if (OrderActQuantity != StockSumCount)
                {
                    e.Appearance.ForeColor = Color.Red;
                }

            }
        }









    }
}